﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using CommonTypes;
using System.Net;
namespace Collertor
{
    public class WorkThread
    {
        private CommonTypes.TargetItem _targetItem;
        private System.Net.WebClient _webclient;
        private bool _isdebug;
        private string _log;

        public event DOnLogging OnLogging;

        public event DOnJobFinished OnFinished;

        public WorkThread(TargetItem _target)
        {
            _targetItem = _target;
            _webclient = new System.Net.WebClient();
            _isdebug = false;
            _log = "";
        }

        public WorkThread()
        {
            _webclient = new System.Net.WebClient();
            _isdebug = false;
            _log = "";
        }

        ~WorkThread()
        {
            _webclient.Dispose();
        }

        public bool Isdebug
        {
            get
            {
                return _isdebug;
            }
            set
            {
                _isdebug = value;
            }
        }

        public string Log
        {
            get
            {
                return _log;
            }
        }

        public CommonTypes.TargetItem TargetItem
        {
            get
            {
                return _targetItem;
            }
            set
            {
                _targetItem=value;
            }
        }

        public System.Net.WebClient WCObj
        {
            get
            {
                return _webclient;
            }
            set
            {
                _webclient = value;
            }
        }

        /// <summary>
        /// 检查必备参数的有效性
        /// </summary>
        private bool CheckParam()
        {
            try
            {
                if (_targetItem.matcheditems.Count < 1) return false;
            }
            catch (Exception e) {
                return false;
            }
            return true;

        }

        public void GetBody()
        {
           // if (CheckParam() == false) return;
            if (_webclient == null) {
                _webclient = new System.Net.WebClient();
            }
             try
            {
                _webclient.Encoding = TargetItem.encoding;
                _webclient.DownloadStringAsync(new Uri(_targetItem.url));
                _webclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(OnAnsyComplete);

                if (_isdebug) doLoging(new LogEventArgs("WorkThread", "已经在获取中"));
            }catch(Exception e){
                if (_isdebug) doLoging(new LogEventArgs("Webclient", e.Message));
            }
        }                                                           

        private void doLoging(LogEventArgs e)
        {
            if (OnLogging != null) {
                OnLogging(e);
            }
        }

        private void OnAnsyComplete(Object sender, DownloadStringCompletedEventArgs e)
        {
            if (!e.Cancelled && e.Error == null)
            {
                _targetItem.body = e.Result;
                DoOnFinishJob(this,new JobFinishedEventArgs(_targetItem));
            }
        }

        private void DoOnFinishJob(Object sender, JobFinishedEventArgs e)
        {
            if (OnFinished != null) {
                OnFinished(e);
            }
        }

    }
}
